package top.xzxsrq.web.download;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;
import top.xzxsrq.ffmpeg.FFmpegUtils;
import top.xzxsrq.utils.PathUtils;
import top.xzxsrq.web.download.entity.PageInfo;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 用于单个视频下载
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class VideoD extends CommonD {
    @Override
    public String getDownloadType() {
        return "单个视频下载";
    }

    /**
     * @param videoUrl 可以用的视频直连
     * @param savePath 保存的文件夹
     * @param saveName 保存的名字
     * @throws InterruptedException
     * @throws ExecutionException
     */
    public void downloadVideo(
            String videoUrl,
            String savePath,
            String saveName
    ) throws InterruptedException, ExecutionException {
        VideoDT videoDT = new VideoDT();
        videoDT.setDownloadType(this.getDownloadType());
        videoDT.setSavePath(savePath);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setDownloadUrl(videoUrl);
        pageInfo.setDownloadName(saveName);
        videoDT.setPageInfo(pageInfo);
        videoDT.setDownloadId(this.downloadTaskId);
        videoDT.setSavePath(PathUtils.pathResolve(savePath, saveName + ".mp4"));
        Future<Void> submit = DownloadManager.submit(videoDT);
        submit.get();
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @Log4j2
    public static class VideoDT extends DownloadThread {
        @Override
        public void createProcess() {
            String downloadUrl = pageInfo.getDownloadUrl();
            process = FFmpegUtils.download(downloadUrl, savePath);
        }

        @Override
        public void showProcessLog(String line) {
            log.info("{}:下载id：{}:{}", downloadType, downloadId, line);
            FFmpegUtils.downloadPer(pageInfo, line);
        }
    }
}
